from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
from faker import Faker


class TestContact:
    def setup_class(self):
        self.fake = Faker("zh_CN")
    def setup(self):
        caps = {}
        # adb logcat ActivityManager:I | findstr "cmp"
        caps["platformName"] = "android"
        caps["deviceName"] = "emulator-5554"
        caps["appPackage"] = "com.tencent.wework"
        caps["appActivity"] = ".launch.WwMainActivity"
        caps["noReset"] = "true"
        caps["ensureWebviewsHavePages"] = True
        self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub",caps)
        #隐式等待
        self.driver.implicitly_wait(5)

    def teardown(self):
        #销毁 driver
        self.driver.quit()

    def test_addcontact(self):
        #随机添加人名和手机号
        name = self.fake.name()
        phone = self.fake.phone_number()
        #点击通讯录
        self.driver.find_element(MobileBy.XPATH,"//*[@text='通讯录']").click()
        #点击添加成员
        self.driver.find_element(MobileBy.XPATH,"//*[@text='添加成员']").click()
        #点击手动输入添加
        self.driver.find_element(MobileBy.XPATH, "//*[@text='手动输入添加']").click()
        #输入姓名
        self.driver.find_element(MobileBy.XPATH,"//*[contains(@text,'姓名')]/../android.widget.EditText").send_keys(name)
        #输入手机号
        self.driver.find_element(MobileBy.XPATH, "//*[contains(@text,'手机')]/..//android.widget.EditText").send_keys(phone)
        #点击保存
        self.driver.find_element(MobileBy.ID,'ad2').click()
        result = self.driver.find_element(MobileBy.XPATH,"//*[@class='android.widget.Toast']").get_attribute("text")
        #判断是否添加成功
        assert "添加成功" == result